﻿using System;
using System.ComponentModel.Composition;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using MediaAssistant.Controls.Dialog;
using MediaAssistant.Controls.WaitScreen;
using MediaAssistant.Helper;
using MefBasic;
using MediaAssistant.DAL.Helper;

namespace MediaAssistant.Controls.SendFeedback
{
    [Export]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class SendFeedbackPresenter:APresenter<SendFeedbackView>, IDialogContentPresenter
    {
        [ImportingConstructor]
        public SendFeedbackPresenter(SendFeedbackView view) : base(view)
        {
        }

        public bool OnClosing(DialogResult dialogResult)
        {
            if(dialogResult==DialogResult.Ok)
            {
                if(AllowContact)
                {
                    if(string.IsNullOrWhiteSpace(FromEmailAddress))
                    {
                        System.Windows.MessageBox.Show(
                            "To contact you, we need your email address. Please enter your email address.",
                            "Empty Email Address", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                        return false;
                    }
                    if (!Regex.IsMatch(FromEmailAddress, @"^([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4})$"))
                    {
                        System.Windows.MessageBox.Show(
                            "The email address you entered is invalid. Please enter a valid email address.",
                            "Invalid Email Address", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                        return false;
                    }
                    if(string.IsNullOrWhiteSpace(Subject) && CrashBugMessageVisibility==Visibility.Collapsed)
                    {
                        System.Windows.MessageBox.Show(
                            "Please enter the subject of your feedback",
                            "Empty Subject", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                        return false;
                    }
                }
            }
            return true;
        }


        public bool AllowContact
        {
            get { return (bool)GetValue(AllowContactProperty); }
            set { SetValue(AllowContactProperty, value); }
        }

        public static readonly DependencyProperty AllowContactProperty =
            DependencyProperty.Register("AllowContact", typeof(bool), typeof(SendFeedbackPresenter), new UIPropertyMetadata(true));

        public void SendException(Exception exception)
        {
            Logger.Instance.Write(exception);
            CrashBugMessageVisibility = Visibility.Visible;
            var messageBox = Resolve<IDialogPresenter>();
            messageBox.OkButtonCaption = "Send";
            Subject = string.Format("Exception \"{0}\" at Media Assistant {1}", exception.Message, SystemHelper.GetVersion());
            FromEmailAddress = RegistryHelper.FromEmailAddress;

            if (messageBox.ShowDialog("Send Crash Report", this) != DialogResult.Ok) return;

            var messageBody = string.Format("Media Assistant Team,\r\nException \"{0}\" occurred at Media Assistant {1}\r\n\r\n", exception.Message, SystemHelper.GetVersion()); ;
            if(AllowContact)
            {
                messageBody += string.Format("Please contact me about this report at {0}\r\n\r\n", FromEmailAddress);
            }
            
            messageBody += string.Format("Exception details\r\n{0}", Logger.GetExceptionMessage(exception));
            messageBody += string.Format("User's Comment\r\n{0}", Message);
            Resolve<WaitScreenPresenter>().Show();
            EMailHelper.Send(FromEmailAddress,Subject, messageBody);
            RegistryHelper.FromEmailAddress = FromEmailAddress;
            Resolve<WaitScreenPresenter>().Hide();
        }

        public void SendFeedback()
        {
            var messageBox = Resolve<IDialogPresenter>();
            messageBox.OkButtonCaption = "Send";
            FromEmailAddress = RegistryHelper.FromEmailAddress;
            CrashBugMessageVisibility = Visibility.Collapsed;
            
            if (messageBox.ShowDialog("Send Feedback", this) != DialogResult.Ok) return;

            var messageBody = string.Format("Media Assistant Team, Feedback message for Media Assistant {0}\r\n\r\n", SystemHelper.GetVersion()); ;
            if (AllowContact)
            {
                messageBody += string.Format("Please contact me about this report at {0}\r\n\r\n",FromEmailAddress);
            }
            messageBody += string.Format("User's Comment\r\n{0}", Message);
            var subject = string.Format("Feedback for Media Assistant {0} - {1}",SystemHelper.GetVersion(), Subject);
            Resolve<WaitScreenPresenter>().Show();
            EMailHelper.Send(FromEmailAddress, subject, messageBody);
            RegistryHelper.FromEmailAddress = FromEmailAddress;
            Resolve<WaitScreenPresenter>().Hide();
        }

        public Visibility CrashBugMessageVisibility
        {
            get { return (Visibility)GetValue(CrashBugMessageVisibilityProperty); }
            set { SetValue(CrashBugMessageVisibilityProperty, value); }
        }

        public static readonly DependencyProperty CrashBugMessageVisibilityProperty =
            DependencyProperty.Register("CrashBugMessageVisibility", typeof(Visibility), typeof(SendFeedbackPresenter), new UIPropertyMetadata(Visibility.Collapsed));

        public string FromEmailAddress
        {
            get { return (string)GetValue(FromEmailAddressProperty); }
            set { SetValue(FromEmailAddressProperty, value); }
        }

        public static readonly DependencyProperty FromEmailAddressProperty =
            DependencyProperty.Register("FromEmailAddress", typeof(string), typeof(SendFeedbackPresenter), new UIPropertyMetadata(string.Empty));

        public string Subject
        {
            get { return (string)GetValue(SubjectProperty); }
            set { SetValue(SubjectProperty, value); }
        }

        public static readonly DependencyProperty SubjectProperty =
            DependencyProperty.Register("Subject", typeof(string), typeof(SendFeedbackPresenter), new UIPropertyMetadata(string.Empty));

        public string Message
        {
            get { return (string)GetValue(MessageProperty); }
            set { SetValue(MessageProperty, value); }
        }

        public static readonly DependencyProperty MessageProperty =
            DependencyProperty.Register("Message", typeof(string), typeof(SendFeedbackPresenter), new UIPropertyMetadata(string.Empty));


    }
}
